New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CakeNumber::fromReadableSize() and Validation::filesize() #850
Conversation
if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) { | ||
return $size * 1; | ||
} | ||
throw new CakeException(__d('cake_dev', 'No unit type.')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a default return? throwing an exception sounds a bit too much for view related code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default return would be a function param
I think this is a useful change and having additional basic validation methods for file uploads should make handling file uploads simpler for users. |
$units = array('KB', 'MB', 'GB', 'TB', 'PB'); | ||
foreach ($units as $i => $unit) { | ||
if ($unit == substr($size, -2)) { | ||
return $size * pow(1024, $i + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't remove the unit from $size
?
Btw, to simplify a bit, you could get the last 2 chars outside, once size never changes. Also, you could override the foreach
using array_search
+ if
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ceeram The array_search portion looks good. But the multiplication point still kind of weird to me. For example, if $size
is 10MB
, you will have '10MB' * pow(1024, 1 + 1)
. Seems it is working, but is kind of weird to do math with 'MB' in the variable.
Btw, to avoid a issue in the future, what you think in use strtoupper
in the unit? It will allow '10mb' as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, seems odd to calcaluate with MB in the strings, pow() converts all to numeric, but i think its clearer if i take it out.
Also will make it strtoupper
+1 for this change. |
Add CakeNumber::fromReadableSize() and Validation::filesize()
I still had this hanging around